package com.monkeygroup.mygoplus.http;

import android.util.Log;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.monkeygroup.mygoplus.base.ThingsModel;
import com.monkeygroup.mygoplus.thing.vo.PriceuseVO;

import java.util.Iterator;
import java.util.Map;

/**
 * Created by lcw on 2017/5/11.
 */

public class ThingsHttpModel {
    private ThingsHttpModel.SimpleOnHttpRequestListener mRequestListener;
    private ThingsHttpModel.PriceuseSimpleOnHttpRequestListener mPriceuseRequestListener;
    protected CustomStringRequest mJsonRequest;
    protected String mRequestUrl = "";
    protected boolean isTokenSave = false;
    protected Map<String, String> mRequestParams = null;
    protected int mRequestMethod = Request.Method.GET;
    public final static int TIMEOUTMS = 30000;
    /***
     * 网络请求GET方式,token参数默认传true
     ****/
    public ThingsHttpModel(String url, ThingsHttpModel.SimpleOnHttpRequestListener listener, boolean isTokenSave) {
        Log.e("urlurl===",url);
        this.mRequestListener = listener;
        this.mRequestUrl = url;
        this.isTokenSave = isTokenSave;
    }

    /****
     * 网络请求POST方式,token参数默认传true
     ****/
    public ThingsHttpModel(String url, Map<String, String> params, ThingsHttpModel.SimpleOnHttpRequestListener listener, boolean isTokenSave) {
        this.mRequestListener = listener;
        this.mRequestUrl = url;
        this.mRequestParams = params;
        this.mRequestMethod = Request.Method.POST;
        this.isTokenSave = isTokenSave;
    }
    /****
     * 网络请求POST方式,token参数默认传true
     ****/
    public ThingsHttpModel(String url, Map<String, String> params, ThingsHttpModel.PriceuseSimpleOnHttpRequestListener listener, boolean isTokenSave) {
        Log.e("urlurl:::",url);
        this.mPriceuseRequestListener = listener;
        this.mRequestUrl = url;
        this.mRequestParams = params;
        this.mRequestMethod = Request.Method.POST;
        this.isTokenSave = isTokenSave;
    }

    /**
     * 请求服务器时间
     */
    public void requestMyGoTime() {
        mJsonRequest = new CustomStringRequest(mRequestMethod, mRequestUrl,
                mRequestParams, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {

                ThingsModel resultModel = handleResponse(response);
                Log.e("结果-->","status:"+resultModel.status+"timestamp:"+resultModel.timestamp+"property::"+resultModel.getProperty());
                mRequestListener.onRequestFinished(resultModel.status, resultModel.timestamp);

            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                mRequestListener.onRequestFinished("ERROR_UNCONNECT", 0);
            }
        });

        // 设置网络超时时间
        mJsonRequest.setRetryPolicy(new DefaultRetryPolicy(TIMEOUTMS, 1, 1.0f));
        HttpRequestManager.addHttpRequest(mJsonRequest);
    }

    /**
     * 请求物件信息
     */
    public void requestThings() {
        mJsonRequest = new CustomStringRequest(mRequestMethod, mRequestUrl,
                mRequestParams, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {

//                Iterator<Map.Entry<String, String>> iterator = mRequestParams.entrySet().iterator();
//                while (iterator.hasNext()) {
//                    Log.e("請求參數","上啦111");
//                    Map.Entry<String, String> entry = iterator.next();
//
//                    Log.e("請求參數==","key = " + entry.getKey() + " and value = " + entry.getValue());
//                }

                ThingsModel resultModel = handleResponse(response);
                Log.e("结果-->","status:"+resultModel.status);
                mRequestListener.onRequestFinished(resultModel.status, resultModel.property,
                        resultModel.user,resultModel.users,resultModel.circuit,resultModel.img);
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                mRequestListener.onRequestFinished("ERROR_UNCONNECT", 0);
            }
        });

        // 设置网络超时时间
        mJsonRequest.setRetryPolicy(new DefaultRetryPolicy(TIMEOUTMS, 1, 1.0f));
        HttpRequestManager.addHttpRequest(mJsonRequest);
    }

    /**
     * 请求物件新建案信息
     */
    public void buildingRequestThings() {
        mJsonRequest = new CustomStringRequest(mRequestMethod, mRequestUrl,
                mRequestParams, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {

                /*Iterator<Map.Entry<String, String>> iterator = mRequestParams.entrySet().iterator();
                while (iterator.hasNext()) {
                    Log.e("請求參數","上啦111");
                    Map.Entry<String, String> entry = iterator.next();

                    Log.e("請求參數==","key = " + entry.getKey() + " and value = " + entry.getValue());
                }*/

                ThingsModel resultModel = handleResponse(response);
                Log.e("结果-->","status:"+resultModel.status);
                mRequestListener.onBuildingRequestFinished(resultModel.status, resultModel.building,
                        resultModel.user,resultModel.users,resultModel.layout,resultModel.img);
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                mRequestListener.onRequestFinished("ERROR_UNCONNECT", 0);
            }
        });

        // 设置网络超时时间
        mJsonRequest.setRetryPolicy(new DefaultRetryPolicy(TIMEOUTMS, 1, 1.0f));
        HttpRequestManager.addHttpRequest(mJsonRequest);
    }

    /**
     * 请求社區信息
     */
    public void communityRequestThings() {
        mJsonRequest = new CustomStringRequest(mRequestMethod, mRequestUrl,
                mRequestParams, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {

                Iterator<Map.Entry<String, String>> iterator = mRequestParams.entrySet().iterator();
                while (iterator.hasNext()) {
                    Log.e("請求參數","上啦111");
                    Map.Entry<String, String> entry = iterator.next();

                    Log.e("請求參數==","key = " + entry.getKey() + " and value = " + entry.getValue());
                }

                ThingsModel resultModel = handleResponse(response);
                Log.e("结果-->","status:"+resultModel.status);
                mRequestListener.onCommunityRequestFinished(resultModel.status, resultModel.community,
                        resultModel.user,resultModel.users,resultModel.circuit,resultModel.img);
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                mRequestListener.onRequestFinished("ERROR_UNCONNECT", 0);
            }
        });

        // 设置网络超时时间
        mJsonRequest.setRetryPolicy(new DefaultRetryPolicy(TIMEOUTMS, 1, 1.0f));
        HttpRequestManager.addHttpRequest(mJsonRequest);
    }




    private ThingsModel handleResponse(String jsonString) {
        Gson gson = new Gson();
        Log.e("jsonStringjsonString==",""+jsonString);
        ThingsModel model = gson.fromJson(jsonString, new TypeToken<ThingsModel>() {
        }.getType());
        Log.e("result::::", ""+model.getStatus());
        Log.e("resultdata::::", ""+model.getProperty());
        Log.e("resultBuilding::::", ""+model.getBuilding());
        Log.e("resultCommunity::::", ""+model.getCommunity());
        Log.e("resultImg::::",""+model.getImg());
        Log.e("resultLayout:::",""+model.getLayout());
        return model;
    }

    public interface SimpleOnHttpRequestListener {
        void onRequestFinished(String resultCode, Integer timestamp);
        void onRequestFinished(String resultCode, Object property, Object user, Object users, Object circuit, Object img);
        void onBuildingRequestFinished(String resultCode, Object building, Object user, Object users, Object circuit, Object img);
        void onCommunityRequestFinished(String resultCode, Object community, Object user, Object users, Object circuit, Object img);
    }

    /**
     * 请求成交行情信息
     */
    public void priceuseRequestThings() {
        mJsonRequest = new CustomStringRequest(mRequestMethod, mRequestUrl,
                mRequestParams, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {

                PriceuseVO resultModel = priceuseHandleResponse(response);
//                Log.e("结果-->","status:"+resultModel.status);
                mPriceuseRequestListener.onRequestFinished(resultModel.status, resultModel.priceuse,
                        resultModel.priceusecnt,resultModel.areausetext,resultModel.areause,resultModel.areagrid,resultModel.areausecnt,resultModel.ticks,resultModel.timedata);
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                mPriceuseRequestListener.onRequestFinished("ERROR_UNCONNECT", 0);
            }
        });

        // 设置网络超时时间
        mJsonRequest.setRetryPolicy(new DefaultRetryPolicy(TIMEOUTMS, 1, 1.0f));
        HttpRequestManager.addHttpRequest(mJsonRequest);
    }

    private PriceuseVO priceuseHandleResponse(String jsonString) {
        Gson gson = new Gson();
        //  Log.e("jsonStringjsonString==",""+jsonString);
        PriceuseVO model = gson.fromJson(jsonString, new TypeToken<PriceuseVO>() {
        }.getType());

        Log.e("result::::", ""+model.getStatus());
        Log.e("getPriceuse::::", ""+model.getAreause());
        Log.e("getPriceusecnt::::", ""+model.getPriceusecnt());

        return model;
    }


    public interface PriceuseSimpleOnHttpRequestListener {
        void onRequestFinished(String resultCode, Integer timestamp);
        void onRequestFinished(String resultCode,Object[] priceuse, Object priceusecnt, Object areausetext, Object[] areause,Object[] areagrid, Object areausecnt,Object[] ticks,Object[] timedata);
    }
}
